﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataFormat.aspx.cs" Inherits="ZX.WebApp.EditWidows.DataFormat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/reset-fonts-grids/reset-fonts-grids.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/container/assets/skins/sam/container.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/menu/assets/skins/sam/menu.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/button/assets/skins/sam/button.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/resize/assets/skins/sam/resize.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/layout/assets/skins/sam/layout.css" />
    <link type="text/css" rel="stylesheet" href="../Scripts/yui_2.9.0/build/datatable/assets/skins/sam/datatable.css" />
    <style type="text/css">
    .yui-skin-sam table {
	    width: 100%;
    }

    #button-example-form fieldset, 
    #button-example-form fieldset div {

        border: 2px groove #ccc;
        margin: .5em;
        padding: .5em;

    }

    .yui-button#pushbutton2 button,
    .yui-button#pushbutton5 button, 
    .yui-button .Add button {
        background: url(../button/assets/add.gif) center center no-repeat;
        text-indent: -4em;
        overflow: hidden;
        padding: 0 .75em;
        width: 2em;
        *margin-left: 4em;   /* IE only */
        *padding: 0 1.75em;  /* IE only */

    }
    
    .yui-button#pushbutton3 button,
    .yui-button#pushbutton6 button, 
    .yui-button#pushbutton9 button {

        padding-left: 2em;
        background: url(../button/assets/add.gif) 10% 50% no-repeat;
    
    }
    <% =DataTableCss %>
</style>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/element/element-min.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/datasource/datasource-min.js"></script>
    <!-- OPTIONAL: JSON Utility (for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <!-- OPTIONAL: Connection Manager (enables XHR for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <!-- OPTIONAL: Get Utility (enables dynamic script nodes for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/get/get-min.js"></script>
    <!-- OPTIONAL: Drag Drop (enables resizeable or reorderable columns) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/dragdrop/dragdrop-min.js"></script>
    <!-- OPTIONAL: Calendar (enables calendar editors) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/calendar/calendar-min.js"></script>
    <!-- Source files -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/datatable/datatable-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/layout/layout-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/button/button-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/container/container-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/resize/resize-min.js"></script>
    <script src="../Scripts/Appjs/model.TrSubjectInfo.js" type="text/javascript"></script>
</head>
<body class="yui-skin-sam">
    <form id="form1" runat="server">
    <div id="listpage">
        <div id="toolbar" style="margin: 2px;">
            <button type="button" id="btnAddNew" name="btnAddNew">
                新增</button>
            <button type="button" id="btnEdit" name="btnEdit">
                编辑</button>
            <button type="button" id="btnDelete" name="btnDelete">
                删除</button>
            <button type="button" id="btnQuery" name="btnQuery">
                查找</button>
            <%   if (EntityName == "TrAttach")
                 { %>
            <button type="button" id="btnDownLoad" name="btnDownLoad">
                资料下载</button>
            <% } %>
            <%   if (EntityName == "TrActivity")
                 { %>
            <button type="button" id="btnSel" name="btnSel">
               选择课程</button>
            <% } %>
        </div>
        <div id="data">
            <div id="json">
            </div>
        </div>
        <div id="bottom">
        </div>
    </div>
    <div id="dialog1" class="yui-pe-content">
        <div class="hd">
           数据编辑</div>
        <div class="bd">
            <iframe id="EditUIWindow" frameborder="0" src="" style="width: 100%; height: 100%;
                margin: 0px; padding: 0px;"></iframe>
        </div>
    </div>
    <div id="dialog2" class="yui-pe-content">
            <div class="hd">
                数据编辑</div>
            <div class="bd">
                <iframe id="EditUIWindow1" frameborder="0" src="" style="width: 100%; height: 100%;
                    margin: 0px; padding: 0px;"></iframe>
            </div>
        </div>
    </form>
</body>
<script type="text/javascript">
    var const_entityID = "<% =EntityId %>",
        const_entityName = "<% =EntityName %>",
        const_EditEntity = "<% =EditEntity %>",
        selectControl=null;
    
    (function () {
        var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;

        Event.onDOMReady(function () {
            var layout = new YAHOO.widget.Layout({
                units: [
                { position: 'top',height: 35, body: 'toolbar', gutter: '1 1 1 1', scroll: null, zIndex: 2 },
                //{ position: 'left', header: '功能模块', width: 250, body: 'left', gutter: '1 5 1 1', collapse: true, scroll: true, zIndex: 1, resize: true },
                {position: 'center', body: 'data', gutter: '1 1', scroll: true },
                { position: 'bottom', height: 30, body: 'bottom', gutter: '1 1'}
            ]
            });
            layout.render();
        });

        

    })();

  
    //新增
    var fnAdd = function(){
        document.getElementById("EditUIWindow").src="../Extend/AutoEditUI.aspx?r="+Math.random()+"&entity="+const_EditEntity;
        YAHOO.example.container.dialog1.show();
    }

    //删除
    function DeleteData(SaveUrl,func)
    {
            var recs = this.mDataTable.getRecordSet();
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
   
                        if(str=="1"){
                            alert("数据删除成功");
                            if(typeof(func)=="function"){
                            func();
                            }
                            //parent.closeWin();
                        }
                        else
                        {
                            alert(str+"\n"+"数据删除失败!");
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
    }
    


    //删除 
    var fnDelete = function(){
            var recordID = myDataTable.getSelectedRows()[0];
            record = myDataTable.getRecord(recordID);
            DeleteData("../Extend/AjaxData/DataSave.aspx?r=" + Math.random()+"&action=delete&entity="+const_EditEntity+"&id="+record.getData(const_entityID),LoadData);           
    }
    //编辑
    var fnEdit = function(key){
        var recs = this.mDataTable.getRecordSet();
        for(var i=0;i<recs._records.length;i++)
        {
            if(recs._records[i]._oData["iSun_Selected"])
            {
                //alert(recs._records[0]._oData[const_entityID]);
            }
        }
        var recordID = myDataTable.getSelectedRows()[0];
        record = myDataTable.getRecord(recordID);
        document.getElementById("EditUIWindow").src="../Extend/AutoEditUI.aspx?r="+Math.random()+"&entity="+const_EditEntity+"&id="+record.getData(const_entityID);
        YAHOO.example.container.dialog1.show();
    }

    //查询
    var fnQuery = function(){
    }
       
    //资料下载
    var pdId;
    var pAddress;
    var fnDownLoad = function(){
        var recordID = myDataTable.getSelectedRows()[0];
        record = myDataTable.getRecord(recordID);
        pdId = record.getData("<%=EntityId%>"); 
        pAddress = record.getData("Address");
        document.getElementById("EditUIWindow").src="DownLoad.aspx?filename="+pAddress;
        YAHOO.example.container.dialog1.hidden();
    }
     //选择课程
     var pId;
     var conditonId;
    var selectId;
     var fnSelectCourse = function(){
        var recordID = myDataTable.getSelectedRows()[0];
        record = myDataTable.getRecord(recordID);
        pId = record.getData("<%=EntityId%>"); 
        conditonId = "TrActivityID";
        selectId = "TrCourseID";   
        document.getElementById("EditUIWindow").src="YUIMenuList.aspx?masterentity=TrActivityCourse&masterPara=TrCourse|CourseName|TrCourseID";
        YAHOO.example.container.dialog1.show();
    }
    YAHOO.example.init = function () {
                 
        // "click" event handler for each Button instance
        function onButtonClick(p_oEvent) {                            
                 YAHOO.log("You clicked button: " + this.get("id"), "info", "example1");
                 switch(this.get("id"))
                 {         
                    case "btnAddNew":
                         fnAdd();
                         break;
                    case "btnEdit":
                         fnEdit();
                          break;
                    case "btnDelete":
                         fnDelete();
                         break;
                    case "btnQuery":
                         fnQuery();
                         break;
                    case "btnExport":
                         fnExport();
                         break;
                    case "btnReport":
                         fnReport();
                         break;
                    case "btnDownLoad":
                        fnDownLoad();
                        break;
                    case "btnSel": 
                        fnSelectCourse();
                        break;
                    default:
                         fnAdd();
                         break;
                 }                        
        }

        // "contentready" event handler for the "pushbuttonsfrommarkup" <fieldset>

        YAHOO.util.Event.onContentReady("toolbar", function () {
            // Create Buttons using existing <input> elements as a data source
            var btnAddNew = new YAHOO.widget.Button("btnAddNew");
            var btnEdit = new YAHOO.widget.Button("btnEdit");
            var btnDelete = new YAHOO.widget.Button("btnDelete");
            var btnQuery = new YAHOO.widget.Button("btnQuery"); 
            btnAddNew.on("click", onButtonClick);
            btnEdit.on("click", onButtonClick);
            btnDelete.on("click", onButtonClick);
            btnQuery.on("click", onButtonClick);
            <% if (EntityName == "TrAttach"){ %>
            var btnDownLoad = new YAHOO.widget.Button("btnDownLoad");
            btnDownLoad.on("click",onButtonClick);
            <% } if(EntityName == "TrActivity"){ %>
            var btnSel = new YAHOO.widget.Button("btnSel"); 
            btnSel.on("click",onButtonClick);
            <%} %>
        });


    } ();

    function AddButton() {
        var button = document.createElement('button');
        button.innerHTML = 'add tab';
        YAHOO.util.Event.on(button, 'click', addTab);
        tabView.appendChild(button);
    }


    //DataTable 
    var mDataTable;
    //刷新数据
    function LoadData()
    {
       BindData();
    }

    //关闭窗口
    function closeWin(){
        LoadData();
        YAHOO.example.container.dialog1.hide();
    }
    // 对应的方法和ID
    var _fn,_id;
    function ShowDialog(fn,id)
    {
         _fn = fn;
         _id = id;
         //selectControl=hidControl;
         YAHOO.example.container.dialog2.show();
    }

    //数据绑定
    function BindData() {
        YAHOO.example.XHR_JSON = function() {
            var formatUrl = function(elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = "<a href='" + oRecord.getData("ClickUrl") + "' target='_blank'>" + sData + "</a>";
            };
        
            var formatRating = function(elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = (sData === null) ? "N/A" : YAHOO.util.Number.format(sData);
            };

            var myColumnDefs = [
                 <% =DataFields %>
            ];

            var myDataSource = new YAHOO.util.DataSource("../Extend/AjaxData/JSONList.aspx?");
            myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
            myDataSource.connXhrMode = "queueRequests";
            myDataSource.responseSchema = {
                resultsList: "rows",
                //fields: ["EntityName", "THCaption", "Field", { key: "Rating.AverageRating", parser: "number" }, "Width"]
                fields: [<% =DataColums %>]
            };
        
            this.myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs,
                    myDataSource,{initialRequest: "entity=" + const_entityName + "&order=<% =Order %>&r="+Math.random()+"&zip=94089&results=10&output=json",selectionMode:"single"  },{ height:"100%"}  );
            mDataTable =this.myDataTable;
            var mySuccessHandler = function() {
                this.set("sortedBy", null);
                this.onDataReturnAppendRows.apply(this,arguments);
            };
            var myFailureHandler = function() {
                this.showTableMessage(YAHOO.widget.DataTable.MSG_ERROR, YAHOO.widget.DataTable.CLASS_ERROR);
                this.onDataReturnAppendRows.apply(this,arguments);
            };
            var callbackObj = {
                success : mySuccessHandler,
                failure : myFailureHandler,
                scope : myDataTable
            };
            

            // 调用自定义双击行的方法
            var onRowDoubleClick = function(){
                //if(typeof(CustomDbClick)=="function")
                //{
                        //CustomDbClick();
                //}
                var recordID = myDataTable.getSelectedRows()[0];
                var record = myDataTable.getRecord(recordID);
                var pId = record.getData("<%=EntityId%>");
                var selectRows = myDataTable.getSelectedRows();
                if(selectRows.length!=null && selectRows.length==1)
                {     
                    var EntityName = "<%=EntityName %>";
                    CustomDbClick(EntityName,pId);
                }
                else{
                    alert("请选中一行");
                }
            };
    
    
	        // Subscribe to events for row selection 
	        this.myDataTable.subscribe("rowMouseoverEvent", this.myDataTable.onEventHighlightRow); 
	        this.myDataTable.subscribe("rowMouseoutEvent", this.myDataTable.onEventUnhighlightRow); 
            this.myDataTable.subscribe("rowClickEvent", this.myDataTable.onEventSelectRow); 
            this.myDataTable.subscribe("rowDblclickEvent", onRowDoubleClick);
            this.myDataTable.subscribe("checkboxClickEvent", function(oArgs){
                var elCheckbox = oArgs.target;
                var oRecord = this.getRecord(elCheckbox);
                oRecord.setData("iSun_Selected",elCheckbox.checked);
            });

            // Programmatically select the first row 
            this.myDataTable.selectRow(this.myDataTable.getTrEl(0)); 
            hideCol();
        }();
    }
    //隐藏/栏位
    function hideCol(){ mDataTable.hideColumn(parseInt(0)); mDataTable.refreshView(); };

    YAHOO.util.Event.addListener(window, "load", BindData);
    YAHOO.namespace("example.container");
    YAHOO.util.Event.onDOMReady(function () {
	
	    // Define various event handlers for Dialog
	    var handleSubmit = function() {
            var editwindow = document.getElementById("EditUIWindow");
            if(editwindow)
            {
               editwindow.contentWindow.Save();
            }
	    };
    
	    var handleCancel = function() {
		    this.cancel();
	    };
        var handleSubmit1=function(){;
            
            this.cancel();

        }
	    var handleSuccess = function(o) {
		    var response = o.responseText;
		    response = response.split("<!")[0];
		    document.getElementById("resp").innerHTML = response;
	    };
	    var handleFailure = function(o) {
		    alert("Submission failed: " + o.status);
	    };
 
   
 
        // Remove progressively enhanced content class, just before creating the module
        YAHOO.util.Dom.removeClass("dialog1", "yui-pe-content");
                    // Instantiate the Dialog
	    YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1", 
							    { width : "<% =EditWindowWidth %>px",
							      height: "<% =EditWindowHeight %>px",
                                  fixedcenter: true,
                                  visible: false,
                                  draggable: true,
                                  close: true,
                                  autofillheight: "body", 
							      constraintoviewport : true,
							      buttons : [ { text:"保存", handler:handleSubmit, isDefault:true },
								          { text:"取消", handler:handleCancel } ]
							    });
      YAHOO.example.container.dialog2= new YAHOO.widget.Dialog("dialog2", 
							    { width : "420px",
							      height: "400px",
                                  fixedcenter: true,
                                  visible: false,
                                  draggable: true,
                                  close: true,
                                  autofillheight: "body", 
							      constraintoviewport : true,
							      buttons : [ { text:"保存", handler:handleSubmit1, isDefault:true },
								          { text:"取消", handler:handleCancel } ]
							    });

    
	    // Validate the entries in the form to require that both first and last name are entered
	    YAHOO.example.container.dialog1.validate = function() {
		    var data = this.getData();
		    if (data.firstname == "" || data.lastname == "") {
			    alert("Please enter your first and last names.");
			    return false;
		    } else {
			    return true;
		    }
	    };
 
	    // Wire up the success and failure handlers
	    YAHOO.example.container.dialog1.callback = { success: handleSuccess,
						         failure: handleFailure };
	
	    // Render the Dialog
	    YAHOO.example.container.dialog1.render();
        YAHOO.example.container.dialog2.render();
 
	    YAHOO.util.Event.addListener("btnAddNew", "click", YAHOO.example.container.dialog1.show, YAHOO.example.container.dialog1, true);
	    //YAHOO.util.Event.addListener("hide", "click", YAHOO.example.container.dialog1.hide, YAHOO.example.container.dialog1, true);

        //拖动大小
		var resize = new YAHOO.util.Resize("dialog1", {
			handles: ["br"],
			autoRatio: false,
			minWidth: 450,
			minHeight: 450,
			status: false
		});
		resize.on("startResize", function (args) {

			if (this.cfg.getProperty("constraintoviewport")) {
				var D = YAHOO.util.Dom;

				var clientRegion = D.getClientRegion();
				var elRegion = D.getRegion(this.element);

				resize.set("maxWidth", clientRegion.right - elRegion.left - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
				resize.set("maxHeight", clientRegion.bottom - elRegion.top - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
			} else {
				resize.set("maxWidth", null);
				resize.set("maxHeight", null);
			}

		}, YAHOO.example.container.dialog1, true);
		resize.on("resize", function (args) {
			var panelHeight = args.height;
			this.cfg.setProperty("height", panelHeight + "px");
		}, YAHOO.example.container.dialog1, true);
    });
</script> 
</html>
